/**
 * 类名：用户个人系统业务类
 * 备注：负责用户个人系统的业务逻辑。
 * 作者：rankin
 * 日期：2016/03/4
 */
var Page = require('../models/page');
var Log = require('../models/log');
var Fault = require('../models/fault');
var Billing = require('../models/billing');
var Box = require('../models/boxes');
var Buyer = require('../models/buyer');
var BoxBuyer = require('../models/box_buyer');
var Sim = require('../models/sim');

var async = require('async');

module.exports = {
    billing: function (req, res) {
        var boxid = req.query.boxid;
        var account = req.query.account;
        new Billing().findSpriceById(boxid, function (datas) {
            if (datas == "") {
                res.render('box/billing', {
                    boxid: boxid,
                    account: account,
                    pri: 0
                });
            } else {
                var standard = datas[0].standard;
                var price = datas[0].price;
                var less = datas[0].less;
                var lessprice = datas[0].lessprice;
                var more = datas[0].more;
                var moreprice = datas[0].moreprice;
                res.render('box/billing', {
                    boxid: boxid,
                    account: account,
                    pri: 1,
                    standard: standard,
                    price: price,
                    less: less,
                    lessprice: lessprice,
                    more: more,
                    moreprice: moreprice
                });
            }
        });
    },
    dobilling: function (req, res) {
        var boxid = req.body.boxid;
        var account = req.body.account;
        var standard = req.body.standard;
        var timetype = req.body.timetype;
        var price = req.body.price;
        var less = req.body.less;
        var lesstimetype = req.body.lesstimetype;
        var lessprice = req.body.lessprice;
        var more = req.body.more;
        var moretimetype = req.body.moretimetype;
        var moreprice = req.body.moreprice;
        if (less == "" && more == "") {
            if (timetype == 1) {
                new Billing().removepriceById(boxid, function (err) {
                    var stan = standard * 60;
                    new Billing({
                        box_id: boxid,
                        standard: stan,
                        price: price,
                        less: less,
                        lessprice: lessprice,
                        more: more,
                        moreprice: moreprice
                    }).save();
                });

            } else if (timetype == 0) {
                new Billing().removepriceById(boxid, function (err) {
                    new Billing({
                        box_id: boxid,
                        standard: standard,
                        price: price,
                        less: less,
                        lessprice: lessprice,
                        more: more,
                        moreprice: moreprice
                    }).save();
                });
            }
        } else if (less == "" && more !== "") {
            if (timetype == 1) {
                var stans = standard * 60;
                if (moretimetype == 1) {
                    new Billing().removepriceById(boxid, function (err) {
                        var mor = more * 60;
                        new Billing({
                            box_id: boxid,
                            standard: stans,
                            price: price,
                            less: less,
                            lessprice: lessprice,
                            more: mor,
                            moreprice: moreprice
                        }).save();
                    });

                } else if (moretimetype == 0) {
                    new Billing().removepriceById(boxid, function (err) {
                        new Billing({
                            box_id: boxid,
                            standard: stans,
                            price: price,
                            less: less,
                            lessprice: lessprice,
                            more: more,
                            moreprice: moreprice
                        }).save();
                    });
                }
            } else if (timetype == 0) {
                if (moretimetype == 1) {
                    new Billing().removepriceById(boxid, function (err) {
                        var mor = more * 60;
                        new Billing({
                            box_id: boxid,
                            standard: standard,
                            price: price,
                            less: less,
                            lessprice: lessprice,
                            more: mor,
                            moreprice: moreprice
                        }).save();
                    });

                } else if (moretimetype == 0) {
                    new Billing().removepriceById(boxid, function (err) {
                        new Billing({
                            box_id: boxid,
                            standard: standard,
                            price: price,
                            less: less,
                            lessprice: lessprice,
                            more: more,
                            moreprice: moreprice
                        }).save();
                    });
                }
            }
        } else if (less !== "" && more == "") {
            if (timetype == 1) {
                var sta = standard * 60;
                if (lesstimetype == 1) {
                    new Billing().removepriceById(boxid, function (err) {
                        var les = less * 60;
                        new Billing({
                            box_id: boxid,
                            standard: sta,
                            price: price,
                            less: les,
                            lessprice: lessprice,
                            more: mor,
                            moreprice: moreprice
                        }).save();
                    });

                } else if (lesstimetype == 0) {
                    new Billing().removepriceById(boxid, function (err) {
                        new Billing({
                            box_id: boxid,
                            standard: sta,
                            price: price,
                            less: less,
                            lessprice: lessprice,
                            more: more,
                            moreprice: moreprice
                        }).save();
                    });
                }
            } else if (timetype == 0) {
                if (lesstimetype == 1) {
                    new Billing().removepriceById(boxid, function (err) {
                        var le = less * 60;
                        new Billing({
                            box_id: boxid,
                            standard: standard,
                            price: price,
                            less: le,
                            lessprice: lessprice,
                            more: more,
                            moreprice: moreprice
                        }).save();
                    });

                } else if (moretimetype == 0) {
                    new Billing().removepriceById(boxid, function (err) {
                        new Billing({
                            box_id: boxid,
                            standard: standard,
                            price: price,
                            less: less,
                            lessprice: lessprice,
                            more: more,
                            moreprice: moreprice
                        }).save();
                    });
                }
            }
        } else if (less !== "" && more !== "") {
            if (timetype == 1) {
                var st = standard * 60;
                if (lesstimetype == 1) {
                    var lesss = less * 60;
                    if (moretimetype == 1) {
                        new Billing().removepriceById(boxid, function (err) {
                            var moree = more * 60;
                            new Billing({
                                box_id: boxid,
                                standard: st,
                                price: price,
                                less: lesss,
                                lessprice: lessprice,
                                more: moree,
                                moreprice: moreprice
                            }).save();
                        });

                    } else if (moretimetype == 0) {
                        new Billing().removepriceById(boxid, function (err) {
                            new Billing({
                                box_id: boxid,
                                standard: st,
                                price: price,
                                less: lesss,
                                lessprice: lessprice,
                                more: more,
                                moreprice: moreprice
                            }).save();
                        });
                    }
                } else if (lesstimetype == 0) {
                    if (moretimetype == 1) {
                        new Billing().removepriceById(boxid, function (err) {
                            var moreee = more * 60;
                            new Billing({
                                box_id: boxid,
                                standard: st,
                                price: price,
                                less: less,
                                lessprice: lessprice,
                                more: moreee,
                                moreprice: moreprice
                            }).save();
                        });

                    } else if (moretimetype == 0) {
                        new Billing().removepriceById(boxid, function (err) {
                            new Billing({
                                box_id: boxid,
                                standard: st,
                                price: price,
                                less: less,
                                lessprice: lessprice,
                                more: more,
                                moreprice: moreprice
                            }).save();
                        });
                    }
                }
            } else if (timetype == 0) {
                if (lesstimetype == 1) {
                    var lessss = less * 60;
                    if (moretimetype == 1) {
                        new Billing().removepriceById(boxid, function (err) {
                            var moreeeee = more * 60;
                            new Billing({
                                box_id: boxid,
                                standard: standard,
                                price: price,
                                less: lessss,
                                lessprice: lessprice,
                                more: moreeeee,
                                moreprice: moreprice
                            }).save();
                        });

                    } else if (moretimetype == 0) {
                        new Billing().removepriceById(boxid, function (err) {
                            new Billing({
                                box_id: boxid,
                                standard: standard,
                                price: price,
                                less: lessss,
                                lessprice: lessprice,
                                more: more,
                                moreprice: moreprice
                            }).save();
                        });
                    }
                } else if (lesstimetype == 0) {
                    if (moretimetype == 1) {
                        new Billing().removepriceById(boxid, function (err) {
                            var moreeeeee = more * 60;
                            new Billing({
                                box_id: boxid,
                                standard: standard,
                                price: price,
                                less: less,
                                lessprice: lessprice,
                                more: moreeeeee,
                                moreprice: moreprice
                            }).save();
                        });

                    } else if (moretimetype == 0) {
                        new Billing().removepriceById(boxid, function (err) {
                            new Billing({
                                box_id: boxid,
                                standard: standard,
                                price: price,
                                less: less,
                                lessprice: lessprice,
                                more: more,
                                moreprice: moreprice
                            }).save();
                        });
                    }
                }
            }
        }
        req.flash('success', '费用设置成功.');
        return res.redirect('/user_system/buyer_box?num=1&&boxid=' + boxid + '&&account=' + account);
    }
};

